package com.mzy.linear_struct.hot100.二叉树;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: mizy3
 * @Date: 2024/08/10/下午7:53
 * @Description:
 */
public class Pro114二叉树展开为链表 {
    TreeNode prev;

    private void flatten(TreeNode root) {
        if (root == null) return;

        flatten(root.right);
        flatten(root.left);
        root.left = null;
        root.right = prev;
        prev = root;
    }


/*   public void flatten(TreeNode root) {
        LinkedList<TreeNode> list = new LinkedList<>();
        toLinkList(root, list);

        TreeNode head = list.removeFirst();
        root = head;
        while (!list.isEmpty()) {
            head.left = null;
            head.right = list.removeFirst();

            head = head.right;
        }

    }

    private void toLinkList(TreeNode root, List<TreeNode> res) {
        if (root == null) {
            return;
        }

        res.add(root);
        toLinkList(root.left, res);
        toLinkList(root.right, res);
    }

    TreeNode pre = null;
    private void rightLeftRoot(TreeNode root) {
        if (root == null) {
            return;
        }

        rightLeftRoot(root.right);
        rightLeftRoot(root.left);
        root.left = null;
        root.right = pre;
        pre = root;

    }*/

    /* TreeNode pre = null;

     public void flatten(TreeNode root) {
         if (root == null) {
             return;
         }

         flatten(root.right);
         flatten(root.left);
         root.right = pre;
         root.left = null;
         pre = root;
     }
 */
    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(1, new TreeNode(2), new TreeNode(5));
        new Pro114二叉树展开为链表().flatten(treeNode);
    }



}
